/*
Copyright 2010 Christian Dadswell
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package subscriber.chrisdadswell.co.uk;

import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.ContextMenu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnCreateContextMenuListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class Activity_Tags extends ListActivity {
	private static final String _APP_TAG = "SUBSCRIBER";
    public static final String _ACT_TAG = "Activity_Tags";
 
	public static int _RESULT_TAGS_FOUND = 0;

	public static String _USERNAME = null;
	public static String _USERID = null;
	public static String _PASSWORD = null;
	public static String _TAG_TITLE = "Untagged";
	
	SharedPreferences _PREFS;
	private ProgressDialog PD;
    
	protected void onCreate(Bundle savedInstanceState) {
		Log.d(_APP_TAG, ".......... onCreate()");
		super.onCreate(savedInstanceState);

		// INITIALIZE PREFERENCES
		_PREFS = PreferenceManager.getDefaultSharedPreferences(this);
		
		final String[] _TAG_TITLES  = Func_JSOUP.getTagList(_USERNAME, _PASSWORD);	
		
		setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, _TAG_TITLES));
		getListView().setTextFilterEnabled(true);
		getListView().setSmoothScrollbarEnabled(true);
		getListView().setFadingEdgeLength(60);
		getListView().setFastScrollEnabled(true);
		getListView().setVerticalFadingEdgeEnabled(true); 
		  
		// CREATE CONTEXT MENU
		getListView().setOnCreateContextMenuListener(new OnCreateContextMenuListener() {
			
			public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
				menu.setHeaderTitle("Tag Options");
				menu.add(0, Constants.CONTEXT_TAG_RENAME, 0, "Rename Tag");
				menu.add(0, Constants.CONTEXT_TAG_DELETE, 0, "Delete Tag");
				menu.add(0, Constants.CONTEXT_TAG_SHARE, 0, "Delete Tag");
			}
		});
	}
	
	@Override
	protected void onListItemClick(ListView listView, View view, int position, long id) {
	    super.onListItemClick(listView, view, position, id);

	    // Get the item that was clicked 
	    Object pos = this.getListAdapter().getItem(position); 
	    String selectedTag = pos.toString();
	    
	    /*
	    Intent intentSubs = new Intent();
	    intentSubs.setClass(view.getContext(), Activity_Subs.class);
	    intentSubs.putExtra("SelectedTag", selectedTag);
	    startActivity(intentSubs);
	    */
	}

	@Override
	public boolean onContextItemSelected(MenuItem item) {
		AdapterView.AdapterContextMenuInfo menuInfo = (AdapterView.AdapterContextMenuInfo)item.getMenuInfo();
		
		String tagTitle = ((TextView) menuInfo.targetView).getText().toString();
		
		switch (item.getItemId()) {
		case Constants.CONTEXT_TAG_RENAME:
			
			break;
		case Constants.CONTEXT_TAG_DELETE:
			
			break;
		case Constants.CONTEXT_TAG_SHARE:
			
			break;

		default:
			return super.onContextItemSelected(item);
		}
		return true;
	}
	
	private void FetchTags_Thread() {
		Log.d(_APP_TAG, "Fetch Tags thread ...");
		PD = ProgressDialog.show(Activity_Tags.this, "", "\nFetching Tags ...\n", true, false);
   		new Thread() { 
			public void run() {
   				try{
   					if(Func_JSOUP.tagsInReader(_USERNAME, _PASSWORD)) {
   						_RESULT_TAGS_FOUND = 1; 
   					}else{
   						_RESULT_TAGS_FOUND = 0;
   					}
   				} catch (Exception e) {  }
   				Handler_FetchTags.sendEmptyMessage(0);
   				PD.dismiss();               
   				}
   		}.start();		
	}

	private Handler Handler_FetchTags = new Handler() {
		public void handleMessage(Message msg) {
			if(_RESULT_TAGS_FOUND == 1) {
				
			}else{
				Toast.makeText(Activity_Tags.this, "\nNo Tags found in your Google Reader account\n", Toast.LENGTH_LONG).show();
			}
		}
	};

	
} // END OF CLASS